package com.gogowan.agroenterprise.storage;

import java.util.ArrayList;
import java.util.List;

import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.base.TitleActivity;
import com.gogowan.agroenterprise.contorl.WeakRefAsyncTask;
import com.gogowan.agroenterprise.contorl.WeakRefAsyncTask.AsyncLoadListener;
import com.gogowan.agroenterprise.contorl.WeakRefAsyncTask.InterActionModel;
import com.gogowan.agroenterprise.model.BindCodeInfoRequest;
import com.gogowan.agroenterprise.model.CodeInfoResult;
import com.gogowan.agroenterprise.model.LoginResultInfo;
import com.gogowan.agroenterprise.model.Message;
import com.gogowan.agroenterprise.model.ResultInfo;
import com.gogowan.agroenterprise.model.SplitBoxGetCodeResult;
import com.gogowan.agroenterprise.utils.PalUtils;
import com.gogowan.agroenterprise.utils.Utils;
import com.gogowan.agroenterprise.widget.adapter.UnpackAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class UnCribActivity extends TitleActivity implements AsyncLoadListener {

	private LoginResultInfo loginResult;
	private SplitBoxGetCodeResult bindCodeRemoveResult;
	private UnpackAdapter unpackAdapter;
	private WeakRefAsyncTask weakRefAsyncTask = new WeakRefAsyncTask(this);
	private static final int COMMIT_RQT = 1;

	@Override
	protected int setContentViewID() {
		return R.layout.activity_unpack;
	}

	@Override
	protected boolean initData(Intent intent, Bundle savedInstanceState) {
		loginResult = PalUtils.getLoginResultInfo(getApplicationContext());
		bindCodeRemoveResult = (SplitBoxGetCodeResult) intent.getSerializableExtra(PalUtils.PACK_MODEL_ITT);
		return loginResult != null && bindCodeRemoveResult != null;
	}

	@Override
	protected void initView() {
		initTitle("拆垛", false);
		findViewById(R.id.layout_cpmc).setVisibility(View.GONE);
		((TextView) findViewById(R.id.unpack_xmnr_tv0)).setText("垛码内容:");
		((TextView) findViewById(R.id.unpack_xmnr_tv)).setText(bindCodeRemoveResult.getCode().getCode());
		((TextView) findViewById(R.id.unpack_cpmc_tv)).setText(bindCodeRemoveResult.getCode().getMaterialFullName());
		unpackAdapter = new UnpackAdapter(this, bindCodeRemoveResult.getListCode(), R.layout.layout_unpack_item);
		ListView listView = (ListView) findViewById(R.id.unpack_lv);
		listView.setAdapter(unpackAdapter);
		findViewById(R.id.unpack_commit_btn).setOnClickListener(this);
		Button btnCommit = (Button) findViewById(R.id.unpack_commit_btn);
		btnCommit.setText("确认拆垛");
	}

	@Override
	protected void onClicked(View v) {
		switch (v.getId()) {
		// 确认关联
		case R.id.unpack_commit_btn:
			commit();
			break;
		}
	}

	@Override
	public void handleMessage(TitleActivity activity, android.os.Message msg) {

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		weakRefAsyncTask.cancel();
	}

	@Override
	public void onResult(AsyncLoadListener asyncLoadListener, String response, int actionId) {
		hideProgressDialog();
		if (isAlreadyDestroyed()) {
			return;
		}
		if (PalUtils.isEmpty(response)) {
			PalUtils.showShortToast(getApplicationContext(), getString(R.string.server_error));
		} else {
			switch (actionId) {
			// 提交
			case COMMIT_RQT:
				ResultInfo resultInfo = (ResultInfo) PalUtils.getPostResult(response, ResultInfo.class);
				if (resultInfo == null) {
					PalUtils.showShortToast(getApplicationContext(), getString(R.string.analyse_error));
				} else if (resultInfo.getResult() > 0) {
					PalUtils.showShortToast(getApplicationContext(), resultInfo.getResultinfo());
					finish();
				} else {
					PalUtils.showShortToast(getApplicationContext(), resultInfo.getResultinfo());
				}
				break;
			}
		}
	}

	/**
	 * 提交
	 */
	private void commit() {
		if (bindCodeRemoveResult.getListCode() == null || bindCodeRemoveResult.getListCode().size() == 0) {
			PalUtils.showShortToast(getApplicationContext(), getString(R.string.sales_msg_default_status));
			return;
		}
		showProgressDialog(getString(R.string.committing));
		List<String> listCode = new ArrayList<>();
		for (CodeInfoResult codeInfoResult : bindCodeRemoveResult.getListCode()) {
			listCode.add(codeInfoResult.getCode());
		}
		BindCodeInfoRequest bindCodeInfoRequest = new BindCodeInfoRequest();
		//bindCodeInfoRequest.setEnterpriseId(loginResult.getEnterpriseinfo().getEnterpriseid());
		//bindCodeInfoRequest.setUserId(loginResult.getUserinfo().getPersonid());
		bindCodeInfoRequest.setBoxCode(bindCodeRemoveResult.getCode().getCode());
		bindCodeInfoRequest.setListCode(listCode);
		String json = Utils.toJSON(bindCodeInfoRequest);
		Message message = new Message(PalUtils.MsgType.ConfirmSplitCribCode, PalUtils.ENCRYPT, json);
		InterActionModel interActionModel = new InterActionModel();
		interActionModel.setIndex(COMMIT_RQT);
		interActionModel.setUrl(getString(R.string.first_url) + getString(R.string.first_url_page));
		interActionModel.setMessage(message);
		weakRefAsyncTask.execute(interActionModel);
	}

}
